/*
 * @Author: your name
 * @Date: 2021-02-15 19:53:05
 * @LastEditTime: 2021-05-18 23:18:53
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \jianfa-api\dao\projectCodeDao.js
 */
const { querySql } = require("../db");
const { page_sql } = require("../utils/utils");
module.exports = {
    // 添加项目
    async addProjectCodeDao({ code_name, html, content, create_time, imgId }) {
        const sql = `insert into project_code (code_name, html , content , create_time , imgId , sort) values ('${code_name}' , '${html}' , '${content}' , '${create_time}','${imgId}', '${Date.now()}')`;
        return await querySql(sql);
    },
    //获取项目pid
    async getPidDao() {
        const sql =
            "select Auto_increment as pid FROM information_schema.`TABLES` WHERE Table_Schema='qljf' AND table_name = 'project_code' limit 1";
        return await querySql(sql);
    },
    //获取项目列表(有id查询单个 没id查询所有)
    async getProjectListDao(params) {
        const { id } = params;

        // sql = "SELECT a.*,b.* FROM project_code a INNER JOIN img b ON a.imgId=b.id where a.id = '${id}'";
        let sql = id
            ? `SELECT a.*,b.img_path FROM project_code a INNER JOIN imgs b ON a.imgId=b.id where a.id = '${id}'`
            : `SELECT a.*,b.img_path FROM project_code a INNER JOIN imgs b ON a.imgId=b.id`; //其中start是页码，limit是每页显示的条数
        return await querySql(await page_sql(params, sql, "project_code", true));
    },
    //更具id删除项目
    async delProCodeDao(id) {
        let sql = `delete from project_code where id='${id}'`;
        return await querySql(sql);
    },
    //更具pid查询所有该项目的图片路径
    async selectImgByPidDao(pid) {
        let sql = `select img_path from project_code_imgs where pid = '${pid}'`;
        return await querySql(sql);
    },
    //更具pid删除所有该项目的图片数据
    async delImgByPidDao(pid) {
        let sql = `delete from project_code_imgs where pid='${pid}'`;
        return await querySql(sql);
    },
    //编辑项目
    async editProjectDao({ code_name, html, content, id, imgId }) {
        //UPDATE 表名称 SET 列名称 = 新值 WHERE 列名称 = 某值
        const sql = `update project_code set imgId='${imgId}' , code_name='${code_name}' , html='${html}', content='${content}' where id='${id}'`;
        return await querySql(sql);
    },
};
